<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="订单号" prop="orderNumber">
        <el-input v-model="queryParams.orderNumber" placeholder="请输入订单号" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户" prop="orderCustomerName">
        <el-input v-model="queryParams.orderCustomerName" placeholder="请输入客户名称" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客服" prop="orderUserId">
        <el-select v-model="queryParams.orderUserId" placeholder="请选择">
          <el-option v-for="item in userList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="售后问题类型" prop="problemType">
        <el-select v-model="queryParams.problemType" placeholder="请选择售后问题">
          <el-option v-for="dict in dict.type.sys_aftersales_type" :key="dict.label" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="处理状态" prop="statusHandle">
        <el-select v-model="queryParams.statusHandle" placeholder="请选择处理状态">
          <el-option v-for="dict in dict.type.sys_af_type" :key="dict.label" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <!-- 防止仅有一个输入框时光标在那一个输入框里时按回车出现“正在加载系统资源，请耐心等待” -->
      <input hidden />
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['after:sales:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['after:sales:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getDataList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" width="60" />
      <el-table-column label="订单号" prop="orderNumber" width="140" />
      <el-table-column label="客户" prop="orderCustomerName" width="100" />
      <el-table-column label="客服" prop="orderUserName" width="100" />
      <el-table-column label="售后问题类型" align="center" prop="problemType" width="160" />
      <el-table-column label="问题描述" prop="problemDescribe" show-overflow-tooltip />
      <!-- <el-table-column label="售后文件" prop="problemImg"  width="160" >
        <template slot-scope="scope">
          <div @click="opentit(scope.row)">
            <i v-if="scope.row.imglist.length != 0" class="el-icon-picture"
              style="font-size: 36px;cursor: pointer;margin-right: 20px;"></i>
            <i v-if="scope.row.videolist.length != 0" class="el-icon-video-camera-solid"
              style="font-size: 36px;cursor: pointer;"></i>
          </div>
        </template>
</el-table-column> -->

      <el-table-column label="售后文件" width="140" show-overflow-tooltip>
        <template slot-scope="scope">
          <div style="display: flex;">
            <i @click="openFile(its)" style="font-size: 24px;color: #1890ff;cursor: pointer;" v-for="(its, index) in scope.row.problemImg" :key="index" class="el-icon-document"></i>
            <!-- <el-image 
              style="width: 30px; height: 30px;margin-right: 10px;" :src="its" :preview-src-list="scope.row.problemImg">
            </el-image> -->
          </div>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="statusHandle">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_af_type" :value="scope.row.statusHandle" />
        </template>
      </el-table-column>
      <el-table-column label="处理说明" prop="statusHandleContent" show-overflow-tooltip />

      <el-table-column label="创建时间" prop="createTime" width="150" />
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['after:sales:handle']">处理</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['after:sales:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getDataList" />

    <!-- 添加或编辑弹框 -->
    <el-dialog :title="addDialogTitle" :visible.sync="addDialogVisible" width="500px" append-to-body>
      <el-form ref="addFormRef" :model="addDialogFormData" :rules="addDialogFormRules" size="medium"
        label-width="100px">
        <el-form-item label="处理说明" prop="statusHandleContent">
          <el-input type="textarea" v-model="addDialogFormData.statusHandleContent" placeholder="请输入处理说明" clearable>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="handleAddDialogCancel">取消</el-button>
        <el-button type="primary" @click="handleAddDialogConfirm">确定</el-button>
      </div>
    </el-dialog>
    <!-- 添加或编辑弹框 -->
    <el-dialog title="售后文件" :visible.sync="titopen" width="600px" append-to-body>
      <div>
        <div v-if="tentfrom.imglist.length != 0">
          <div style="font-size: 18px;font-weight: bold;margin-bottom: 20px;">图片：</div>
          <div style="display: flex;">
            <div v-for="(item, index) in tentfrom.imglist" :key="index" style="margin-right: 8px;">
              <el-image style="width: 100px; height: 100px;" :src="item" :preview-src-list="tentfrom.imglist">
              </el-image>
            </div>
          </div>
        </div>
        <div v-if="tentfrom.videolist.length != 0">
          <div style="font-size: 18px;font-weight: bold;margin-bottom: 20px;">视频：</div>
          <div style="display: flex;">
            <div v-for="(item, index) in tentfrom.videolist" :key="index" style="margin-right: 8px;">
              <video controls :autoplay="false" :src="item" style="width: 160px; height: 120px;"></video>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 快递平台管理
import { postSalesList, setInfo, postDeliveryRemove, getUserListRole } from '@/api/aftersales'
export default {
  name: 'CustomerList',
  dicts: ['sys_af_type', 'sys_aftersales_type'],
  data() {
    return {
      tentfrom: {
        videolist: [],
        imglist: [],

      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: undefined, // 平台名字
        orderUserName: undefined, // 平台编号
        orderCustomerName: undefined,
        problemType: undefined, // 平台编号
        statusHandle: undefined, // 平台编号
        beginTime: undefined, // 平台编号
        endTime: undefined, // 平台编号
      },
      // 数据列表
      dataList: [],
      // 生产角色下所有用户列表
      scUserList: [],
      // 添加弹框标题
      addDialogTitle: '售后问题处理',
      // 添加弹框是否显示
      addDialogVisible: false,
      titopen: false,
      // 添加弹框表单参数
      addDialogFormData: {},
      // 添加弹框表单校验
      addDialogFormRules: {
        statusHandleContent: [{ required: true, message: '请输入处理说明', trigger: 'blur' }]
      },
      dateRange: [],
      userList: []
    }
  },
  created() {
    this.getDataList()
    this.getUserList()
  },
  methods: {
    openFile(item){
      console.log(item);
      window.open(item)
    },
    getUserList() {
      getUserListRole({
        codes: 'kefu',
      }).then((response) => {
        let list = response.data
        this.userList = list.map(item => {
          return {
            label: item.nickName,
            value: item.userId
          }
        })
        this.userList.unshift({
          label: '超级管理员',
          value: 1
        })

      })
    },
    /** 查询列表 */
    getDataList() {
      this.queryParams.beginTime = this.dateRange[0];
      this.queryParams.endTime = this.dateRange[1];
      this.loading = true
      postSalesList(this.queryParams).then((response) => {
        response.rows.forEach(item => {
          item.problemImg = item.problemImg.split(',')
        });
        this.dataList = response.rows
        console.log(this.dataList);
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getDataList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    handleUpdate(row) {
      this.addDialogFormData.id = row.id
      this.addDialogVisible = true
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let userIds = row.id || this.ids;
      if (typeof userIds == "number") {
        userIds = [userIds];
      }
      this.$modal
        .confirm('是否确认删除ID为"' + userIds + '"的数据项？')
        .then(function () {
          return postDeliveryRemove({ ids: userIds });
        })
        .then(() => {
          this.$modal.msgSuccess("删除成功");
          this.getDataList();

        })
        .catch(() => { });
    },
    handleAddDialogCancel() {
      this.addDialogVisible = false
    },
    /** 提交按钮 */
    handleAddDialogConfirm: function () {
      this.$refs["addFormRef"].validate((valid) => {
        if (valid) {
          setInfo(this.addDialogFormData).then(res => {
            this.$modal.msgSuccess("处理成功");
            this.addDialogVisible = false;
            this.getDataList();
          })
        }
      });
    },
    opentit(row) {
      console.log(row);
      this.tentfrom = row
      this.titopen = true
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "after/sales/export",
        {
          ...this.queryParams,
        },
        `售后管理.xlsx`
      );
    },
  },
}
</script>

<style lang="scss" scoped></style>